﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccess;
using Microsoft.Office.Interop.Access;
using System.Data.OleDb;
using System.Data.Common;
namespace TestForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbConnectionStringBuilder oledbConnBuilder = new System.Data.OleDb.OleDbConnectionStringBuilder();
            oledbConnBuilder.DataSource = "d:\\test.accdb";
            oledbConnBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";

            //添加新的数据库配置，并执行。

            DbAccess.Db.Add("test", "System.Data.OleDb", oledbConnBuilder.ConnectionString, setDefault: true);

            //2013-4-16 增加方法ConnectionAction
            DataTable dtSchema = DbAccess.Default.ExecuteDataTable("select * from test", null, getSchema: true);
            for (int i = 0; i < dtSchema.Rows.Count; i++)
            {
                Debug.WriteLine(dtSchema.Rows[i][0]);
            }

            ////使用reader
            List<string> tableNames = new List<string>();
            DbAccess.Db["test"].ExecuteReader("select name from [table]", item => tableNames.Add(item.GetString(0)));


            //动态标量的方式来使用（若名称类型）
            dynamic db = DbAccess.Db;

            var ttt = db.dmp_release.ExecuteNonQuery("update sys_tables_state set [state]=@state", new Parameter("state", 0));
            db.dmp.ExecuteNonQuery("delete from sys_userlog");


            //强名称类型
            Database db1 = DbAccess.Db["dmp"];
            var cnt1 = db1.ExecuteNonQuery("delete from sys_userlog");
            Debug.WriteLine("delete sys_userlog count:" + cnt1);

            //创建参数
            System.Data.SqlClient.SqlParameter sqlParameter = new System.Data.SqlClient.SqlParameter("state", 0);

            var p = DbAccess.Db["dmp"].CreateParameter("name", 3);
            Debug.WriteLine(p.DbType.ToString());

            //创建多个参数
            var p1 = DbAccess.Db["dmp"].CreateParameters("name,last_update", "test", null);
            Debug.WriteLine(p1.Length);

            int counter = DbAccess.Db["dmp_release"].ExecuteNonQuery("update sys_tables_state set [state]=@state", new Parameter("state", 0));
            counter = DbAccess.Db["dmp_release"].ExecuteNonQuery("update sys_tables_state set [state]=@state", new DbParameter[] { sqlParameter });
            Debug.WriteLine("update count:" + counter);

            counter = DbAccess.Db["dmp_release"].ExecuteNonQuery("update sys_tables_state set [last_update]=@last_update", new Parameter("last_update", null));
            Debug.WriteLine("update count:" + counter);

            //使用默认数据库的方式来执行
            DbAccess.SetDefault("dmp_release");
            var dt = DbAccess.Default.ExecuteDataTable("select * from sys_tables_state where product_name=@product and [state]=@state", new Parameter("product", "PASS"), new Parameter("state", 0));
        }
    }
}
